package cn.tedu.em.maintain.webapi.service.impl;

import cn.tedu.em.common.exception.EstateManagementServiceException;
import cn.tedu.em.common.pojo.maintain.dto.MaintainAddNewDTO;
import cn.tedu.em.common.pojo.maintain.dto.MaintainUpdateDTO;
import cn.tedu.em.common.pojo.maintain.entity.Maintain;
import cn.tedu.em.common.pojo.maintain.vo.MaintainListItemVO;
import cn.tedu.em.common.pojo.maintain.vo.MaintainStandardVO;
import cn.tedu.em.common.restful.ResponseCode;
import cn.tedu.em.maintain.service.IMaintainService;
import cn.tedu.em.maintain.webapi.mapper.MaintainMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@DubboService
@Service
@Slf4j
public class MaintainServiceImpl implements IMaintainService {

    @Autowired
    private MaintainMapper maintainMapper;

    public MaintainServiceImpl() {
        log.info("创建业务类的对象：MaintainServiceImpl");
    }

    @Override
    public void addNew(MaintainAddNewDTO maintainAddNewDTO) {
        log.info("开始处理【添加维修】的业务，参数：{}", maintainAddNewDTO);

        Maintain maintain = new Maintain();
        BeanUtils.copyProperties(maintainAddNewDTO,maintain);
        int unDispose = 0;
        maintain.setDispose(unDispose);
        log.info("即将执行插入数据，参数：{}", maintain);
        int rows = maintainMapper.insert(maintain);
        if (rows != 1){
            String message = "添加维修失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new EstateManagementServiceException(ResponseCode.INTERNAL_SERVER_ERROR, message);
        }
    }

    @Override
    public void delete(Long id) {
        log.info("开始处理【根据ID删除维修】的业务，参数：{}", id);

        MaintainStandardVO maintainStandardVO = maintainMapper.getStandardById(id);
        if (maintainStandardVO == null){
            String message = "删除维修失败，尝试删除的维修数据不存在！";
            log.warn(message);
            throw new EstateManagementServiceException(ResponseCode.NOT_FOUND,message);
        }

        log.debug("即将执行删除，参数：{}", id);
        int rows = maintainMapper.deleteById(id);
        if (rows != 1){
            String message = "删除维修失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new EstateManagementServiceException(ResponseCode.INTERNAL_SERVER_ERROR, message);
        }

    }

    @Override
    public void SetUndispose(Long id) {
        log.debug("开始处理【未处理维修】的业务，参数：{}", id);
        updateDisposeById(id,0);
    }

    @Override
    public void SetDispose(Long id) {
        log.debug("开始处理【已处理维修】的业务，参数：{}", id);
        updateDisposeById(id,1);
    }

    @Override
    public void updateInfoById(Long id, MaintainUpdateDTO maintainUpdateDTO) {
        log.debug("开始处理【修改维修】的业务，ID：{}，新数据：{}", id, maintainUpdateDTO);
        MaintainStandardVO maintainStandardVO= maintainMapper.getStandardById(id);

        if (maintainStandardVO==null){
            String message = "修改维修失败，尝试修改的维修数据不存在！";
            log.warn(message);
            throw new EstateManagementServiceException(ResponseCode.NOT_FOUND,message);
        }

        Maintain maintain = new Maintain();
        BeanUtils.copyProperties(maintainUpdateDTO,maintain);
        maintain.setId(id);
        log.debug("即将修改维修详情：{}", maintain);
        int rows = maintainMapper.update(maintain);
        if (rows != 1){
            String message = "修改维修失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new EstateManagementServiceException(ResponseCode.INTERNAL_SERVER_ERROR, message);
        }
    }

    @Override
    public MaintainStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询维修详情】的业务");
        MaintainStandardVO maintain = maintainMapper.getStandardById(id);
        if (maintain==null){
            String message = "查询维修详情失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new EstateManagementServiceException(ResponseCode.NOT_FOUND,message);
        }
        return maintain;
    }

    @Override
    public List<MaintainListItemVO> list() {
        log.debug("开始处理【查询维修列表】的业务，无参数");
        List<MaintainListItemVO> list = maintainMapper.list();
        return list;
    }

    private void updateDisposeById(Long id, Integer isDispose) {
        log.info("id:{}",id);
        MaintainStandardVO maintain = maintainMapper.getStandardById(id);
        if (maintain == null){
            String message = "修改维修状态失败，维修数据不存在！";
            log.warn(message);
            throw new EstateManagementServiceException(ResponseCode.NOT_FOUND,message);
        }

        if (maintain.getDispose() == isDispose){
            String message = "修改维修状态失败，此维修已经处于" + DISPOSE_TEXT[isDispose] + "状态！";
            log.warn(message);
            throw new EstateManagementServiceException(ResponseCode.CONFLICT,message);
        }

        Maintain updateMaintain = new Maintain();
        updateMaintain.setId(id);
        updateMaintain.setDispose(isDispose);

        int rows = maintainMapper.update(updateMaintain);
        if (rows != 1){
            String message = "修改维修状态失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new EstateManagementServiceException(ResponseCode.INTERNAL_SERVER_ERROR, message);
        }
    }
}
